package com.obas.component.tree {
   import com.ease.util.StringUtils;
   import com.obas.component.tree.treeclass.Node;
   import com.obas.constants.TestingLibConstants;
   import com.obas.view.menu.InspectMenu;
   import com.ease.component.controls.Menu;

   import mx.collections.ArrayCollection;
   import mx.core.Container;

   public class ObjectTree extends LoadingTree {
      public var remoteServiceFunc:Function;


      override protected function buildMenu(container:Container):void {
         var inspectMenu:Menu = new InspectMenu();
         container.addChild(inspectMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_OBJECT] = inspectMenu;
      }

      override protected function callRemoteService(node:Node):void {
         if (remoteServiceFunc != null) {
            remoteServiceFunc(node);
         }
      }

      override public function appendChildren(result:ArrayCollection, parentNode:Node):void {
         removeNodeStat(parentNode);
         var childNode:Node = null;
         for each (var child:* in result) {
            childNode = nodeFactory.createNode(getNodeModel(child));
            childNode.type = TestingLibConstants.ITEMTYPE_OBJECT;
            childNode.parentNode = parentNode;
            childNode.children == child.hasChildren == "true"? new ArrayCollection() : null;
            parentNode.children.addItem(childNode);
         }

         parentNode.initialized = true;
         expandItem(parentNode, false);
         expandItem(parentNode, true);
      }

      private function getNodeModel(entity:*):* {
         var locatorPart:* = entity.locatorPart;
         var automationName:String = locatorPart.automationName + "";
         var automationClassName:String = locatorPart.automationClassName + "";
         automationName = automationName.replace(new RegExp("[\r\n \r \t \n]", "g"), " ");
//         automationName = removeDuplicateBlankSpace(automationName);
         entity.name = automationClassName + "(" + automationName + ")";
         return entity;
      }

      private function removeDuplicateBlankSpace(value:String):String {
         var result:String = value;
         var myPattern:RegExp = /  /g;
         while (result.indexOf("  ") != -1) {
            result = result.replace(myPattern, " ");
         }
         return result;
      }
   }
}